package 优先级队列;

import java.util.*;

/**
 * https://leetcode-cn.com/problems/top-k-frequent-elements/
 */
public class _347_前K个高频元素 {
    public int[] topKFrequent(int[] nums, int k) {
     int[] a = new int[k];
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            Integer count = map.get(nums[i]);
            count = count == null ? 0 : count ;
            map.put(nums[i],count + 1);

        }
        PriorityQueue<Integer> pq = new PriorityQueue<>((o1, o2) -> map.get(o2)-map.get(o1));
        pq.addAll(map.keySet());
        for(int i=0;i<k;i++){
            a[i]=pq.remove();
        }
        return a;
    }
}
